<?php
/*
 * Created on Apr 30, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("dao/IDao.php");
require_once("model/Photo.php");

class PhotoDAO implements IDao{
	var $conn;
	var $column = array("photo_id", "caption", "image_data_large", "image_data_medium", "image_data_thumb", "image_type", "image_size", "image_name","ext");
		
 	public function __construct($conn){
 		$this->conn =& $conn;	
 	}
 	public function getItem($id){
 		$photo = null;
 		try{
 			if($stmt = $this->conn->prepare("CALL PHOTO_READ_ITEM(?)")){
				$stmt->bind_param("i",$id);
				$stmt->execute();
				$stmt->store_result();
				$data = array();
				$params = array();
				foreach($this->column as $col_name){
					$params[] =& $data[$col_name];
				}
				
				$res = call_user_func_array(array($stmt,"bind_result"),$params);
				if(!$res){
					echo "bind_result() failed: ". $this->conn->error ."\n";
				}
				else{
					$res = $stmt->fetch();
					if($res){}else{
						echo $stmt->error;
					}
				}
				
				$photo = new Photo();
				$photo = $this->mapDataAndClass($photo,$data);
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}	
 		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		$stmt->close();
		return $photo;
 		
 	}
 	
 	public function saveItem($obj){}
 	
 	public function deleteItem($photoIdList){
 		if($stmt = $this->conn->prepare("CALL PHOTO_DELETE(?)")){
			$stmt->bind_param("s",$photoIdList);
			$stmt->execute();
			
			$stmt->close();
		}
		else{
			printf("<p>Error in getPhotosByPlayerId.<br/>Detail: %s</p>",$stmt->error);
		}
 	}
 	public function __destruct(){
 		$this->conn->close();
 	}
 	
 	
	
	
	
	
	
	
	private function mapDataAndClass($photo,$data){
		$photo->photoId = $data["photo_id"];
		$photo->caption = $data["caption"];
		$photo->imageDataLarge = $data["image_data_large"];
		$photo->imageDataMed =  $data["image_data_medium"];
		$photo->imageDataThumb =  $data["image_data_thumb"];
		$photo->imageType = $data["image_type"];
		$photo->imageSize = $data["image_size"];
		$photo->imageName = $data["image_name"];
		$photo->ext = $data["ext"];
		return $photo;
	}
}
?>
